package com.yukh.test.javabase.properties;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import org.junit.Test;

/**
 * java.util.Properties test
 * @author yukh
 *
 */
@SuppressWarnings("all")
public class PropertiesDemo {
	
//	@Test
	public void readValue(){
		try {
			InputStream in = this.getClass().getResourceAsStream("test.props");
			Properties pro = new Properties();
			pro.load(in);
			
			printProperties(pro);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
//	@Test
	public void loadFromXml(){
		try {
			InputStream in = this.getClass().getResourceAsStream("test.xml");
			Properties pro = new Properties();
			pro.loadFromXML(in);
			
			printProperties(pro);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void storeXml(){
		try {
			Properties pro = new Properties();
			pro.setProperty("usename", "yukh");
			pro.setProperty("useage", "27");
			pro.setProperty("usesex", "man");
			pro.setProperty("usephone", "13554454323");
			
			FileOutputStream  out = new FileOutputStream("d:/test.xml");
			pro.storeToXML(out, "storeXml");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void printProperties(Properties pro){
		// one
		Enumeration en = pro.propertyNames();
		while(en.hasMoreElements()){
			String key = (String)en.nextElement();
			String value = (String)pro.get(key);
			System.out.println(key + "=" + value);
		}
		
		/*
		// two
		Iterator it = pro.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry me = (Map.Entry)it.next();
			String key = (String)me.getKey();
			String value = (String)me.getValue();
			System.out.println(key + "=" + value);
		}*/
		
		/*
		// three
		Enumeration en = pro.elements();
		while(en.hasMoreElements()){
			String value = (String)en.nextElement();
			System.out.println(value);
		}*/
		
		// four
//		pro.list(System.out);
	}
	
}
